package top.lyjwn.ajui.test;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import top.lyjwn.ajui.R;
import top.lyjwn.ajui.activity.ChatDialogActivity;
import top.lyjwn.ajui.data.User;
import top.lyjwn.ajui.widget.recycler.base.BaseViewHolder;
import top.lyjwn.ajui.widget.recycler.base.RecyclerBaseListAdapter;

public class ChatListAdapter extends RecyclerBaseListAdapter<User> {

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_list_item, parent, false);
        ChatListItemHolder chatListItemHolder = new ChatListItemHolder(inflate);
        chatListItemHolder.setContext(parent.getContext());
        return chatListItemHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
        ChatListItemHolder itemHolder = (ChatListItemHolder)holder;
        User user = TList.get(position);
        if (user!=null){
            itemHolder.chat_list_item_name.setText(user.getName());
            if (user.getId().equals(UserUtil.getUser().getId())){
                itemHolder.chat_list_item_name_click.setText("当然！你也可以和自己聊天(点击)");
            }else{
                itemHolder.chat_list_item_name_click.setText("点击聊天");
            }
            itemHolder.chat_list_item_name_click.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent( holder.getContext(), ChatDialogActivity.class);
                    intent.putExtra("opposite_user_name",user.getName());
                    intent.putExtra("opposite_user_id",user.getId());
                    holder.getContext().startActivity(intent);
                }
            });

        }

    }

    static class ChatListItemHolder extends BaseViewHolder{

        TextView chat_list_item_name;

        TextView chat_list_item_name_click;

        public ChatListItemHolder(View view) {
            super(view);
             chat_list_item_name = view.findViewById(R.id.chat_list_item_name);
            chat_list_item_name_click = view.findViewById(R.id.chat_list_item_name_click);
        }
    }
}
